package com.summer.controller;

import com.auth0.jwt.exceptions.TokenExpiredException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.summer.entity.TbResponse;
import com.summer.entity.TbUserinfo;
import com.summer.entity.UserResponse;
import com.summer.mapper.TbResponseMapper;
import com.summer.mapper.UserResponseMapper;
import com.summer.utils.JwtToken;
import com.summer.utils.MyUtil;
import com.summer.utils.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;

/**
 * 还没测试
 */
@RestController
@RequestMapping("/userResponse")
@Api(value="UserResponseController",description = "用户抢答表")
public class UserResponseController {
    Boolean flag;
    MyUtil myUtil = new MyUtil();
    @Autowired(required = false)
    UserResponseMapper userResponseMapper;
    @Autowired(required = false)
    TbResponseMapper tbResponseMapper;
    /**
 *      private int id;
 *     private int  response_id;
 *     private int user_id;
 *     private String  user_name;
 *     private Date response_time;
 */
    @ApiOperation("用户抢答")
    @PostMapping("/teacher/addResponse")
    @ResponseBody
    public ResponseEntity addResponse(@RequestParam(required = true) int response_id,
                                      @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        TbResponse tbResponse=tbResponseMapper.selectById(response_id);
        if(tbResponse.getIs_open()==1){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","抢答已经关闭");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
        responseEntity = ResponseEntity.ok();
        UserResponse userResponse = new UserResponse();
        userResponse.setResponse_id(response_id);
        userResponse.setResponse_time(new Date());
        userResponse.setUser_id(tbUserinfo.getUserId());
        userResponse.setUser_name(tbUserinfo.getNickname());
        userResponseMapper.insert(userResponse);
        responseEntity.putDataValue("id",userResponse.getId());
        responseEntity.putDataValue("msg","用户抢答添加成功");
        return responseEntity;
    }

    @ApiOperation("查询某个抢答的首个用户名")
    @PostMapping("/teacher/selectResponse")
    @ResponseBody
    public ResponseEntity selectResponse(@RequestParam(required = true) int response_id,
                                      @RequestHeader String Authorization
    ) throws IOException, TokenExpiredException {
        ResponseEntity responseEntity;
        String token = Authorization;
        //parentRoleId
        //判断token是否存在或过期
        if (flag = myUtil.judgeToken(token)){
            responseEntity = ResponseEntity.forbidden();
            responseEntity.putDataValue("msg","token不存在或已过期");
            return responseEntity;
        }
        //TbUserinfo tbUserinfo = JwtToken.unsign(token, TbUserinfo.class);
        //用户角色，分微信用户和老师用户，1代表微信用户，2代表老师用户,默认为微信用户
        TbResponse tbResponse=tbResponseMapper.selectById(response_id);
        if(tbResponse.getIs_open()==1){
            responseEntity = ResponseEntity.unauthorized();
            responseEntity.putDataValue("msg","抢答已经关闭");
            responseEntity.putDataValue("timestamp",myUtil.getTime());
            return responseEntity;
        }
        responseEntity = ResponseEntity.ok();
        QueryWrapper<UserResponse> queryWrapper = new QueryWrapper<>();
        //https://my.oschina.net/u/4933191/blog/4978792Mybatis-Plus 查询获取数据集最大值最小值
        queryWrapper.eq("response_id",response_id);
        queryWrapper.orderByAsc("id");
        queryWrapper.last("limit 1");
        UserResponse userResponse = userResponseMapper.selectOne(queryWrapper);
        responseEntity.putDataValue("UserResponse",userResponse);
        responseEntity.putDataValue("msg","查询某个抢答的首个用户名成功");
        return responseEntity;
    }


}
